package com.sc.VenusQueryBeta.ui.fragments.vip.child;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hengjun.kingstar.sdk.base.BasePresenter;
import com.hengjun.kingstar.sdk.base.fragment.BaseMVPCompatFragment;
import com.hengjun.kingstar.sdk.rxbus.RxBus;
import com.hengjun.kingstar.sdk.utils.LogUtil;
import com.hengjun.kingstar.sdk.utils.SpUtils;
import com.hengjun.kingstar.sdk.utils.ToastUtils;
import com.sc.VenusQueryBeta.R;
import com.sc.VenusQueryBeta.adapter.account.MsgChildAdapter;
import com.sc.VenusQueryBeta.constant.Constants;
import com.sc.VenusQueryBeta.contract.vip.MsgChildContract;
import com.sc.VenusQueryBeta.global.MyApplication;
import com.sc.VenusQueryBeta.model.UpdateChildMsgEvent;
import com.sc.VenusQueryBeta.model.bean.login.UsersLoginBean;
import com.sc.VenusQueryBeta.model.sqlite.DataBaseDao;
import com.sc.VenusQueryBeta.model.sqlite.MsgListBean;
import com.sc.VenusQueryBeta.presenter.vip.MsgChildPresenter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;


/**
 * Created by Administrator on 2018/11/7.
 * item_msgchild
 */

public class MsgChildFragment extends BaseMVPCompatFragment<MsgChildContract.MsgChildPresenter> implements MsgChildContract.IMsgChildView {

    private static MsgChildFragment fragment;
    @BindView(R.id.changepassword_back)
    ImageButton changepasswordBack;
    @BindView(R.id.setting_name)
    TextView settingName;
    @BindView(R.id.rv_vip_list)
    RecyclerView rvVipList;
    private List<MsgListBean> msgListBeans;
    private MsgChildAdapter msgchildadapter;
    private CompositeDisposable compositeDisposable;
    private DataBaseDao dataBaseDao;
    private UsersLoginBean loginBean;
    private String sendId;
    private List<MsgListBean> newmsgListBeans;
    private  ArrayList<MsgListBean> group1;

    public static MsgChildFragment newInstance(int position, List<MsgListBean> msgListBeans) {
        Bundle args = new Bundle();
        args.putSerializable(Constants.MESSAGE, (Serializable) msgListBeans);
        fragment = new MsgChildFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (compositeDisposable != null) {
            RxBus.rxBusUnbund(compositeDisposable);
        }
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        if (msgListBeans != null && msgListBeans.size() != 0) {
        } else {
            ToastUtils.showToast("数据异常");
        }
    }

    /**
     * 初始化数据
     */
    @Override
    public void initData() {
        super.initData();
        loginBean = SpUtils.getObject(MyApplication.getContext(), UsersLoginBean.class);
        compositeDisposable = new CompositeDisposable();
        if (fragment.getArguments() != null) {
            msgListBeans = (List<MsgListBean>) fragment.getArguments().getSerializable(Constants.MESSAGE);
            for (MsgListBean msgListBean : msgListBeans) {
                LogUtil.e("data:" + msgListBean.getMessageContent());
            }
            msgchildadapter = new MsgChildAdapter(R.layout.item_msgchild);
            rvVipList.setAdapter(msgchildadapter);
            rvVipList.setLayoutManager(new LinearLayoutManager(mActivity));
            sendId = msgListBeans.get(0).getSendId();
        }

    }


    @Override
    public void onResume() {
        super.onResume();
        RxBus.getInstance().toObservableSticky(UpdateChildMsgEvent.class)
                .subscribe(new Observer<UpdateChildMsgEvent>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(UpdateChildMsgEvent setShoppingGoodsCount) {
                        dataBaseDao = new DataBaseDao(MyApplication.app, loginBean.getId(), 11);
                        SQLiteDatabase rdb = dataBaseDao.databasehelper.getWritableDatabase();
                        rdb.beginTransaction();
                        try {
                            //批量处理操作
                            LogUtil.e("sendId="+sendId);
                            newmsgListBeans =dataBaseDao.searchMsg1(sendId);
                            rdb.setTransactionSuccessful(); //设置事务处理成功，不设置会自动回滚不提交
                        } catch (Exception e) {
                        } finally {
                            LogUtil.e("newmsgListBeans size="+newmsgListBeans.size());
                            msgchildadapter.replaceData(newmsgListBeans);
                            msgchildadapter.notifyDataSetChanged();
                            rvVipList.scrollToPosition(msgchildadapter.getItemCount() - 1);
                            rdb.endTransaction(); //处理完成
                            rdb.close();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public boolean onBackPressedSupport() {
        pop();
        return true;
    }

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return MsgChildPresenter.newInstance();
    }

    @Override
    public int getLayoutId() {
        return R.layout.msgchild_layout;
    }

    @Override
    public void initUI(View view, @Nullable Bundle savedInstanceState) {
        initRecycleView(msgListBeans);
        rvVipList.scrollToPosition(msgchildadapter.getItemCount() - 1);
        settingName.setText(sendId);
    }


    @OnClick(R.id.changepassword_back)
    public void onViewClicked() {
        pop();
    }

    /**
     * 填充账户列表适配器
     */
    private void initRecycleView(List<MsgListBean> msgListBeans1) {
        msgchildadapter = new MsgChildAdapter(R.layout.item_msgchild, msgListBeans1);
        rvVipList.setAdapter(msgchildadapter);
    }
}
